package com.ourcraft.informationcollectionsystem.controller.province;


import com.alibaba.fastjson.JSONObject;
import com.ourcraft.informationcollectionsystem.service.province.recordExamine.RecordExamineService;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityCode;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityValidator;
import com.ourcraft.informationcollectionsystem.utils.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * RecordExamineController handles Record-Examine-related operations.
 *
 * @author Liu Yiming
 * @since 2023-11-04
 */
@RequestMapping("province/reportExamine/")
@RestController
@CrossOrigin()
@AuthorityValidator(code = AuthorityCode.RECORD_EXAMINE)
public class RecordExamineController {

    @Autowired
    RecordExamineService recordExamineService;

    /**
     * Retrieves information of all records out by user ID.
     *
     * @param userId The ID of the user.
     * @param request Permission verification related.
     * @return information of all records.
     */
    @GetMapping("getAllRecords")
    public Result<ArrayList<Map<String, Object>>> getAllRecords(@RequestParam int userId, HttpServletRequest request)
    {
        ArrayList<Map<String, Object>> result = recordExamineService.getAllRecords(userId);
        return Result.success(result);
    }

    /**
     * Retrieves information of a record out by user ID.
     *
     * @param userId The ID of the user.
     * @param request Permission verification related.
     * @return information of the record.
     */
    @GetMapping("getSingleRecord")
    public Result<Map<String, Object>> getSingleRecord(@RequestParam int userId, HttpServletRequest request)
    {
        Map<String, Object> result = recordExamineService.getSingleRecord(userId);
        return Result.success(result);
    }

    /**
     * pass the record in request.
     *
     * @param data The record to be passed.
     * @param request Permission verification related.
     * @return Operation result.
     */
    @PostMapping("passRecord")
    public Result<Boolean> passRecord(@RequestBody JSONObject data, HttpServletRequest request)
    {
        // 从data中获取companyId
        int companyID = data.getIntValue("companyID");
        boolean success = recordExamineService.passRecord(companyID);
        if (success) {
            return Result.success();
        }
        return Result.fail();
    }

    /**
     * return back the record in request.
     *
     * @param data The record to be returned.
     * @param request Permission verification related.
     * @return Operation result.
     */
    @PostMapping("returnBackRecord")
    public Result<Boolean> returnBackRecords(@RequestBody JSONObject data, HttpServletRequest request)
    {
        // 从data中获取companyId、退回原因以及退回原因详细描述
        int companyID = (int) ((LinkedHashMap)data.get("items")).get("companyID");
        String reason = (String) ((LinkedHashMap)data.get("comments")).get("reason");
        String reasonDescription = (String) ((LinkedHashMap)data.get("comments")).get("otherReasonDescription");
        boolean success = recordExamineService.returnBackRecord(companyID, reason, reasonDescription);
        if (success) {
            return Result.success();
        }
        return Result.fail();
    }
}
